﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Threading;


namespace RoadTeam.FineFile
{
    public partial class New_Release : Form
    {
        public New_Release()
        {
            InitializeComponent();
            LoadPath();
        }

        public void LoadPath()
        {
            Dictionary<string, string> dict = ReadWrite.ReadFile();
            if (dict.Keys.Contains(textBox1.Name))
                textBox1.Text = dict[textBox1.Name];
            if (dict.Keys.Contains(textBox2.Name))
                textBox2.Text = dict[textBox2.Name];
            if (dict.Keys.Contains(textBox3.Name))
                textBox3.Text = dict[textBox3.Name];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("发布网站路径不能为空"); return;
            }

            if (String.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("文件取出路径不能为空"); return;
            }

            if (String.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("TXT文档不能为空"); return;
            }

            try
            {
                //FileOpera fio = new FileOpera();
                //List<String> liPath = ReadWrite.GetPathList(textBox3.Text);
                //IGetPath FullPath = new GetNewFullPath();
                //List<String> PathFileName = FullPath.GetFileFullPath(liPath, textBox1.Text);
                //progressBar1.Value = 0;
                //progressBar1.Step = 1;
                //progressBar1.Maximum = PathFileName.Count;
                //foreach (String item in PathFileName)
                //{
                //    fio.CopyFile(textBox1.Text, textBox2.Text, item);
                //    progressBar1.Value += progressBar1.Step;
                //}


                FileOpera fio = new FileOpera();
                List<String> liPath = ReadWrite.GetPathList(textBox3.Text);
                IGetPath FullPath = new GetNewFullPath();
                List<String> PathFileName = FullPath.GetFileFullPath(liPath, textBox1.Text);

                FileOpera opere;
                Thread[] threas = new Thread[PathFileName.Count];

                progressBar1.Value = 0;
                progressBar1.Step = 1;
                progressBar1.Maximum = PathFileName.Count;

                for (int i = 0; i < threas.Length; i++)
                {
                    opere = new FileOpera();
                    opere.SourceDir = textBox1.Text;
                    opere.OutDir = textBox2.Text;
                    opere.FileDirAndName = PathFileName[i];
                    ThreadStart ths = new ThreadStart(opere.CopyFile);
                    threas[i] = new Thread(ths);
                    threas[i].Start();
                    progressBar1.Value += progressBar1.Step;
                }
                MessageBox.Show("获取文件成功");
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message); return;
            }
        }
        #region 方法已经重构至Code文件夹中

        //private List<String> GetPathList(String Path)
        //{

        //    StreamReader reader = new StreamReader(Path);
        //    List<string> li = new List<string>();
        //    while (!reader.EndOfStream)
        //    {
        //        li.Add(reader.ReadLine().Replace('/', '\\'));
        //    }
        //    return li;
        //}

        //private void CopyFile(string SourceDir, string OutDir, string FileDirAndName)
        //{
        //    //把路径标准化,把/替换为\
        //    FileDirAndName = FileDirAndName.Replace("/", "\\");

        //    String Temp = SourceDir + "\\" + FileDirAndName;
        //    //如果指定的文件不存在,则不创建该目录
        //    if (!File.Exists(Temp)) return;

        //    List<string> DirNameArr = FileDirAndName.Split('\\').ToList();
        //    if (DirNameArr != null || DirNameArr.Count > 0)
        //    {
        //        for (int i = 0; i < DirNameArr.Count; i++)
        //        {
        //            string Item = DirNameArr[i];
        //            if (!string.IsNullOrEmpty(Item))
        //            {
        //                //判断是不是最后一个文件名
        //                if (i == (DirNameArr.Count - 1))
        //                {   //正式复制文件
        //                    CopyFileToTarget(SourceDir, OutDir, Item);
        //                }
        //                //如果是文件夹
        //                else if (!Directory.Exists(OutDir + "\\" + Item) && Directory.Exists(SourceDir + "\\" + Item))
        //                {
        //                    Directory.CreateDirectory(OutDir + "\\" + Item);
        //                }
        //                SourceDir += "\\" + Item;
        //                OutDir += "\\" + Item;
        //            }
        //        }
        //    }
        //}

        //private void CopyFileToTarget(String source, String destination, string CopyFileName)
        //{
        //    try
        //    {
        //        DirectoryInfo info = new DirectoryInfo(source);
        //        FileSystemInfo[] sysInfos = info.GetFileSystemInfos();

        //        foreach (FileSystemInfo fsi in info.GetFileSystemInfos())
        //        {
        //            string destName = Path.Combine(destination, fsi.Name);
        //            if (fsi is System.IO.FileInfo)
        //            {
        //                string sourceName = Path.Combine(source, fsi.Name);
        //                try
        //                {
        //                    //just in case the destination is readonly
        //                    if (File.Exists(destName))
        //                    {
        //                        new FileInfo(destName).IsReadOnly = false;
        //                    }
        //                    if (fsi.Name == CopyFileName)
        //                    {
        //                        File.Copy(fsi.FullName, destName, true);
        //                    }
        //                }
        //                catch (Exception ex)
        //                {
        //                    MessageBox.Show("出错了,错误信息:" + ex.Message);
        //                    return;
        //                }
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("出错了,错误信息:" + ex.Message);
        //        return;
        //    }
        //}

        //private List<String> GetFileFullPath(List<String> liPath, String SourcePath)
        //{
        //    String FileName = String.Empty;
        //    String BinSourcePath = SourcePath;

        //    String Extension = String.Empty;

        //    List<String> PathFileName = new List<String>();
        //    String[] Exception = new string[] { "html", "htm", "css", "js", "dll", "xml" };



        //    foreach (String StrPath in liPath)
        //    {

        //        if (String.IsNullOrEmpty(StrPath)) continue;

        //        FileName = GetFileName(StrPath);

        //        String[] Extensions = FileName.Split('.');
        //        Extension = Extensions[Extensions.Length - 1];

        //        if (Exception.Contains(Extension))
        //        {
        //            PathFileName.Add(StrPath);
        //            continue;
        //        }

        //        String[] fileList = Directory.GetFiles(BinSourcePath + "\\bin\\", FileName + "*");
        //        String virtualPath = String.Empty;
        //        String assembly = String.Empty;
        //        foreach (String FilePath in fileList)
        //        {
        //            if (!GetDllInfo(FilePath, StrPath, ref  virtualPath, ref assembly)) continue;
        //            // if (String.IsNullOrEmpty(assembly)) continue;
        //            PathFileName.Add(StrPath);
        //            PathFileName.Add("bin\\" + GetFileName(FilePath));
        //            PathFileName.Add("bin\\" + assembly + ".dll");
        //            PathFileName.Add("bin\\" + assembly + ".pdb");

        //        }
        //    }
        //    return PathFileName;
        //}

        //private bool GetDllInfo(string FilePath, String StrPath, ref String virtualPath, ref String assembly)
        //{
        //    LoadXML(FilePath, ref virtualPath, ref assembly);

        //    virtualPath = virtualPath.Substring(virtualPath.IndexOf('/', 2) + 1, virtualPath.Length - virtualPath.IndexOf('/', 2) - 1);
        //    virtualPath = virtualPath.Replace('/', '\\');
        //    return virtualPath == StrPath;
        //}

        //private void LoadXML(string FilePath, ref String virtualPath, ref String assembly)
        //{
        //    XmlDocument doc = new XmlDocument();
        //    doc.Load(FilePath);
        //    XmlNode xlm = doc.SelectSingleNode("preserve");

        //    if (xlm.Attributes["assembly"] != null)
        //        assembly = xlm.Attributes["assembly"].Value.ToString();

        //    if (xlm.Attributes["virtualPath"] != null)
        //        virtualPath = xlm.Attributes["virtualPath"].Value.ToString();
        //}

        //private String GetFileName(String StrPath)
        //{
        //    StrPath = StrPath.Replace('/', '\\');
        //    int index = StrPath.LastIndexOf('\\');
        //    return StrPath.Substring(index + 1, StrPath.Length - index - 1);
        //}


        #endregion

        #region 基本按钮信息
        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fd1 = new FolderBrowserDialog();
            if (fd1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = fd1.SelectedPath;
                ReadWrite.WriteFile("textBox1|" + textBox1.Text);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fd1 = new FolderBrowserDialog();
            if (fd1.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = fd1.SelectedPath;
                ReadWrite.WriteFile("textBox2|" + textBox2.Text);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "*.txt|";
            if (op.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = op.FileName;
                ReadWrite.WriteFile("textBox3|" + textBox3.Text);
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ReadWrite.WriteFile("textBox1|" + textBox1.Text);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            ReadWrite.WriteFile("textBox2|" + textBox2.Text);
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            ReadWrite.WriteFile("textBox3|" + textBox3.Text);
        }

        #endregion
    }
}
